package com.gouaixin.bee.beeframework2.model;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.ab.util.AbToastUtil;
import com.gouaixin.bee.beeframework2.MyConfig;
import com.gouaixin.bee.beeframework2.activity.paySuccessActivity2;
import com.gouaixin.bee.beeframework2.bean.AbiType;
import com.gouaixin.bee.beeframework2.bean.BaoyueType;
import com.gouaixin.bee.beeframework2.bean.Goods_id;
import com.gouaixin.bee.beeframework2.bean.HongniangType;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.bean.VipType;
import com.gouaixin.bee.beeframework2.bean.YbiType;
import com.gouaixin.bee.beeframework2.bean.ZhenxinhuaFriend;
import com.gouaixin.bee.beeframework2.bean.getChatFriendsResponse;
import com.gouaixin.bee.beeframework2.dao.zhenxinhuaFriendDao;
import com.gouaixin.bee.beeframework2.extra.floatView.FloaView_MainActivity;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.TimeUtil;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.GetListener;
import cn.bmob.v3.listener.UpdateListener;

//import com.gouaixin.bee.beeframework2.extra.wxpay.simcpux.wxapi.WXEntryActivity;

/**
 * Created by Administrator on 2015/4/17.
 */

//会员控制器（VIP、包月写信、Y币、爱币、红娘）
public class MemberModel {

    //根据支付成功的goods_id,做出相应的处理
    public static void doBusinessWithGoodid(Context context, final int type) {
        Log.e("test","doBusinessWithGoodid:接受的type="+type);
        switch (type){
            case Goods_id.VIP_1888:
                MemberModel.vipSuccess(context, VipType.Type_1888);
                PreferencesUtils.putInt(context, "payAmount", 1888);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,1888);
                break;
            case Goods_id.VIP_500:
                MemberModel.vipSuccess(context, VipType.Type_500);
                PreferencesUtils.putInt(context,"payAmount",500);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,500);

                break;
            case Goods_id.VIP_200:
                MemberModel.vipSuccess(context, VipType.Type_200);
                PreferencesUtils.putInt(context,"payAmount",200);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,200);

                break;
            case Goods_id.VIP_100:
                MemberModel.vipSuccess(context, VipType.Type_100);
                PreferencesUtils.putInt(context,"payAmount",100);

                //根据金额，跳转到送话费界面
                toSongHuafei(context,100);
                break;

            case Goods_id.Baoyue_1888:
                MemberModel.baoyueSuccess(context, BaoyueType.Type_1888);
                PreferencesUtils.putInt(context,"payAmount",1888);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,1888);

                break;
            case Goods_id.Baoyue_300:
                MemberModel.baoyueSuccess(context, BaoyueType.Type_300);
                PreferencesUtils.putInt(context,"payAmount",300);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,300);

                break;
            case Goods_id.Baoyue_100:
                MemberModel.baoyueSuccess(context, BaoyueType.Type_100);
                PreferencesUtils.putInt(context,"payAmount",100);

                //根据金额，跳转到送话费界面
                toSongHuafei(context,100);

                break;
            case Goods_id.Baoyue_50:
                MemberModel.baoyueSuccess(context, BaoyueType.Type_50);
                PreferencesUtils.putInt(context,"payAmount",50);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,50);

                break;

            case Goods_id.Hongniang_100:
                MemberModel.hongniangSuccess(context, HongniangType.Type_100);
                PreferencesUtils.putInt(context,"payAmount",100);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,100);

                break;
            case Goods_id.Hongniang_200:
                MemberModel.hongniangSuccess(context,HongniangType.Type_200);
                PreferencesUtils.putInt(context,"payAmount",200);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,200);

                break;
            case Goods_id.Hongniang_300:
                MemberModel.hongniangSuccess(context,HongniangType.Type_300);
                PreferencesUtils.putInt(context,"payAmount",300);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,300);

                break;


            case Goods_id.Ybi_100:
                MemberModel.ybiSuccess(context, YbiType.Type_100);
                PreferencesUtils.putInt(context,"payAmount",100);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,100);

                break;

            case Goods_id.Ybi_50:
                MemberModel.ybiSuccess(context, YbiType.Type_50);
                PreferencesUtils.putInt(context,"payAmount",50);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,50);

                break;

            case Goods_id.Ybi_30:
                MemberModel.ybiSuccess(context, YbiType.Type_30);
                PreferencesUtils.putInt(context,"payAmount",30);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,30);

                break;

            case Goods_id.Ybi_10:
                MemberModel.ybiSuccess(context, YbiType.Type_10);
                PreferencesUtils.putInt(context,"payAmount",10);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,10);

                break;



            case Goods_id.Aibi_500:
                MemberModel.abiSuccess(context, AbiType.Type_500);
                PreferencesUtils.putInt(context,"payAmount",500);
                //根据金额，跳转到送话费界面
                toSongHuafei(context,500);

                break;

        }

    }

    /**
     * 根据金额，跳转到送话费界面
     * @param i
     */
    private static void toSongHuafei(Context context,int i) {
        if(i>=100){
            PreferencesUtils.putLong(context,"tomorrowData",System.currentTimeMillis()+ MyConfig.Banner);
            Intent k1 = new Intent(context, paySuccessActivity2.class);
            k1.putExtra("amount",i);
            PreferencesUtils.putInt(context, "payAmount", i);
            context.startActivity(k1);
            initBanner(context);
        }


    }

    private static void initBanner(Context context) {

        Intent k1 = new Intent(context, FloaView_MainActivity.class);
        context.startActivity(k1);
    }


    //获取当前登录用户(带回调接口)
    public static void getUSER(Context context, final GetUserLister mGetUserLister) {
        BmobQuery<USER> query = new BmobQuery<USER>();
        query.getObject(context, BmobUser.getCurrentUser(context).getObjectId(), new GetListener<USER>() {
            @Override
            public void onSuccess(USER user) {

                mGetUserLister.onSuccess(user);

            }

            @Override
            public void onFailure(int i, String s) {

            }
        });
    }

    //判断当前登录用户是否充值过(带回调接口)
    public static void haveRecharge(Context context, final HaveRechargeListener listener) {
        BmobQuery<USER> query = new BmobQuery<USER>();
        query.getObject(context, BmobUser.getCurrentUser(context).getObjectId(), new GetListener<USER>() {
            @Override
            public void onSuccess(USER user) {
                if(user.getAbiType()==0&&user.getHongniangType()==0&&user.getVipType()==0&&user.getBaoyueType()==0){
                    listener.onResult(false);//没有充值过
                }else {
                    listener.onResult(true);//之前充值过
                }

            }

            @Override
            public void onFailure(int i, String s) {

            }
        });
    }




    //获取当前登录用户
    public static void getUSER(Context context) {
        BmobQuery<USER> query = new BmobQuery<USER>();
        query.getObject(context, BmobUser.getCurrentUser(context).getObjectId(), new GetListener<USER>() {
            @Override
            public void onSuccess(USER user) {
                if (mGetUserLister != null) {
                    mGetUserLister.onSuccess(user);
                }
            }

            @Override
            public void onFailure(int i, String s) {

            }
        });
    }

    //充VIP成功后
    public static void vipSuccess(final Context context, final int type) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {

                mLog.e("接受到vip=" + type);
                user.setVipType(type);
                long endTime = TimeUtil.getEndTimeWithMouth(type);
                user.setVipEndTime(endTime);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {



                        if(type!=0){
                            AbToastUtil.showToast(context, "你已成为VIP" );
                            mLog.e("重置A+B");
                            resetNumber(context);

                        }

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }

    //充包月写信成功后
    public static void baoyueSuccess(final Context context, final int type) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                user.setBaoyueType(type);
                long endTime = TimeUtil.getEndTimeWithMouth(type);
                user.setBaoyueEndTime(endTime);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "你已开通包月写信" );
                        resetNumber(context);
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);
            }
        });
        getUSER(context);
    }


    /**
     * 充值成功后，重置所有的聊天次数和真心话次数
     * 1.遍历聊天好友列表
     * 2.根据每一个聊天好友的jid设置A和B
     * 4.遍历聊天好友列表
     * 5.根据每一个真心话好友的jid设置A和B
     * 6.重置isOver为false
     * @param context
     */
    private static void resetNumber(final Context context) {
        //1.重置聊天好友
        ChatFriendModel.setGetChatFriendLister(new ChatFriendModel.GetChatFriendLister() {
            @Override
            public void onSuccess(ArrayList<getChatFriendsResponse> datas) {
                mLog.e("1.遍历聊天好友列表：" + datas.size());
                for(int i=0;i<datas.size();i++){
                    getChatFriendsResponse bean=datas.get(i);
                    String jid=bean.getId();
                    mLog.e("聊天好友jid=" + jid);

                    int A = PreferencesUtils.getInt(context, jid + "LiaoCi", 0);
                    int B = PreferencesUtils.getInt(context, jid + "ZhenCi", 0);
                    mLog.e("重置之前的：" + jid + "LiaoCi=" + A + "\t" + "ZhenCi=" + B);

                    PreferencesUtils.putInt(context, jid + "LiaoCi", 0);
                    PreferencesUtils.putInt(context, jid + "ZhenCi", 0);
                    int A2 = PreferencesUtils.getInt(context, jid + "LiaoCi", 0);
                    int B2 = PreferencesUtils.getInt(context, jid + "ZhenCi", 0);
                    mLog.e("重置之后的：" + jid + "LiaoCi=" + A2 + "\t" + "ZhenCi=" + B2);
                }

            }
        });
        ChatFriendModel.getChatFriends(context);

        //3.重置真心话好友.
        List<ZhenxinhuaFriend> datas = new ArrayList<ZhenxinhuaFriend>();
        datas = zhenxinhuaFriendDao.query(context);
        if (datas!=null) {
            for(int i=0;i<datas.size();i++){
                ZhenxinhuaFriend bean=datas.get(i);
                String jid=bean.getJid();
                mLog.e("真心话好友jid=" + jid);

                int A = PreferencesUtils.getInt(context, jid + "LiaoCi", 0);
                int B = PreferencesUtils.getInt(context, jid + "ZhenCi", 0);
                mLog.e("重置之前的：" + jid + "LiaoCi=" + A + "\t" + "ZhenCi=" + B);

                PreferencesUtils.putInt(context, jid + "LiaoCi", 0);
                PreferencesUtils.putInt(context, jid + "ZhenCi", 0);
                int A2 = PreferencesUtils.getInt(context, jid + "LiaoCi", 0);
                int B2 = PreferencesUtils.getInt(context, jid + "ZhenCi", 0);
                mLog.e("重置之后的：" + jid + "LiaoCi=" + A2 + "\t" + "ZhenCi=" + B2);
            }

        }

        //5.重置isOver
        PreferencesUtils.putBoolean(context, "isOver", false);

//        //6.延时20分钟后，启动杀不死的服务
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Log.e("test","启动杀不死的服务:时间="+System.currentTimeMillis());
//                Intent intent = new Intent();
//                intent.setClass(context, MyService.class);
//                context.startService(intent);
//            }
//        }, 20*1000);

        //7.清空Y币
//        removeAllYbi(context);



    }

    //清空Y币
    public static void removeAllYbi(final Context context) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                user.setYbiType(0);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        Log.e("test","清空Y币：成功");
                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }

    //充y币成功后
    public static void ybiSuccess(final Context context, final int type) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                user.setYbiType(type);
                user.setYbiIsStart(true);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        if (type != 0) {
                            AbToastUtil.showToast(context, "你已开通Y币");
                        }

                        resetNumber(context);

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }

    //充爱币成功后
    public static void abiSuccess(final Context context, final int type) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                user.setAbiType(type);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "你已开通爱币" );
                        resetNumber(context);

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }

    //消费Y币
    public static void ybiSubtract(final Context context, final int number) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                int oldNumber = user.getYbiType();

                if (oldNumber < number) {
                    user.setYbiType(0);
                }else{
                    int newNumber = oldNumber - number;
                    user.setYbiType(newNumber);
                }

                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
//                        AbToastUtil.showToast(context, "你已消费爱币" + number);
                        mLog.e("你已消费爱币" + number);

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }

    //消费爱币
    public static void abiSubtract(final Context context, final int number) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                int oldNumber = user.getAbiType();
                int newNumber = oldNumber - number;
                user.setAbiType(newNumber);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
//                        AbToastUtil.showToast(context, "你已消费爱币" + number);
                        mLog.e("你已消费爱币" + number);

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }


    //自定义一个回调方法

    /**
     * 定义一个接口
     */
    public interface GetUserLister {
        public void onSuccess(USER user);

        public void onFailure(int i, String s);
    }

    /**
     * 定义一个接口（是否是充值过）
     */
    public interface HaveRechargeListener {
        public void onResult(boolean haveRecharge);
    }

    /**
     * 初始化接口变量
     */
    private static GetUserLister mGetUserLister = null;

    /**
     * 自定义控件的自定义事件
     */
    public static void setGetUserLister(GetUserLister mGetUserLister2) {
        mGetUserLister = mGetUserLister2;
    }

    //充红娘成功后
    public static void hongniangSuccess(final Context context, final int type) {

        setGetUserLister(new GetUserLister() {
            @Override
            public void onSuccess(USER user) {
                user.setHongniangType(type);
                user.update(context, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        AbToastUtil.showToast(context, "你已开通红娘" + type);
                        resetNumber(context);

                    }

                    @Override
                    public void onFailure(int i, String s) {
                        AbToastUtil.showToast(context, "出现异常：" + s);
                    }
                });
            }

            @Override
            public void onFailure(int i, String s) {
                AbToastUtil.showToast(context, "出现异常：" + s);

            }

        });
        getUSER(context);
    }


}
